//
//  MCRedBagSearchTVController.m
//  ZXZX
//
//  Created by menhao on 2018/8/28.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCRedBagSearchTVController.h"
#import "AppDelegate.h"
#import "CustomTableViewCell.h"
#import "MCSendListNameModel.h"
#import "MCHongBaoPersonTableViewCell.h"

@interface MCRedBagSearchTVController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)  UITableView *tableView;
@property (strong, nonatomic)  MCSendListNameModel *sendListNameModel;
@property (strong, nonatomic) NSMutableArray *searchList;//满足搜索条件的数组
@property (strong, nonatomic) NSMutableArray *dataListArry;//请求到的数据数组
@end

@implementation MCRedBagSearchTVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataListArry = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, G_SCREENHEIGHT - kStatusBarAndNavigationBarHeight - 20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MCHongBaoPersonTableViewCell class] forCellReuseIdentifier:@"hongBaocell"];
    [self.view addSubview:self.tableView];

    self.edgesForExtendedLayout = UIRectEdgeNone;//不加的话，UISearchBar返回后会上移
    [self.tableView setContentInset:UIEdgeInsetsZero];
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    [self setExceptionViewFrame];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;

    [self loadSendNameListData];

    if (IS_iPhoneX) {

        self.searchBar.y = 0;
    }else{
        if (@available(iOS 11.0, *)) {
            self.searchBar.y = 10;
        } else {
            self.searchBar.y = 0;
            self.tableView.heiht = G_SCREENHEIGHT  - 64;
        }
        
    }
   
}


- (void)setExceptionViewFrame{
    CGRect rect = CGRectMake(MC_REALVALUE(13), MC_REALVALUE(13) , G_SCREENWIDTH - MC_REALVALUE(26), G_SCREENHEIGHT-MC_REALVALUE(20) - kStatusBarAndNavigationBarHeight);
    self.noDataWin.frame = rect;
    self.errNetWin.frame = rect;
    self.errDataWin.frame = rect;
}
- (void)loadData{
    [self.dataListArry removeAllObjects];
    [self.tableView reloadData];
    [self hiddenExDataView];
    [self loadSendNameListData];
}
- (void)loadSendNameListData{
    WS(wself);
    
    int  Type ;
    if (self.type == 1 || self.type == 2) {
        Type = 0;
    }else{
        Type = 1;
    }
    self.sendListNameModel.IsTeam = [NSString stringWithFormat:@"%d",Type];
    [self.sendListNameModel refreashDataAndShow];
    self.sendListNameModel.callBackSuccessBlock = ^(id manager) {
        [wself.dataListArry removeAllObjects];
        if (wself.type == 2) {
            
            NSMutableArray *arrM = [MCSendListNameModel mj_objectArrayWithKeyValuesArray:manager[@"UserList"]];
            for (MCSendListNameModel *m in arrM) {
                if (m.IsAgent) {
                    [wself.dataListArry addObject:m];
                }
            }
          
            
        }else{
            wself.dataListArry = [MCSendListNameModel mj_objectArrayWithKeyValuesArray:manager[@"UserList"]];

                if (wself.type == 3) {
                    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
                    NSMutableArray * marr = [[NSMutableArray alloc]initWithArray:wself.dataListArry];
                    for (MCSendListNameModel *m in marr) {
                        if ([name isEqualToString:m.UserName]) {
                            [wself.dataListArry removeObject:m];
                        }
                    }
                }
        }
        
        NSMutableArray *nameArray = [NSMutableArray array];
        for (MCSendListNameModel *model in wself.dataListArry) {
            [nameArray addObject:model.UserName];
        }
        
        wself.searchList = [NSMutableArray arrayWithArray:nameArray];
        if (wself.searchList.count == 0) {
            [wself showExDataView];
        } else {
            [wself hiddenExDataView];
        }
        [wself.tableView reloadData];

         
    };
    WS(weakSelf);
    self.sendListNameModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        if ([errorCode isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = (NSDictionary *)errorCode;
            if ([dic[@"code"] isKindOfClass:[NSError class]]) {
                NSError *err = dic[@"code"];
           
                [weakSelf showExcaptionViewWithErr:err];
                
            }
        }else{

            [SVProgressHUD showInfoWithStatus:manager.responseMessage];
            
        }
    };
    
}





#pragma mark - tableView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return  self.searchList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     MCHongBaoPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hongBaocell"];
     cell.dataSourceName = self.searchList[indexPath.row];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *searchName = self.searchList[indexPath.row];
    for (MCSendListNameModel *model in self.dataListArry) {
        if ([searchName isEqualToString:model.UserName]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"RedBagSearchUserNameNotification" object:model];
            
        }
    }
   
}

#pragma mark - UISearchResultsUpdating
//每输入一个字符都会执行一次
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
   
    searchController.searchResultsController.view.hidden = NO;
    //谓词搜索
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchController.searchBar.text];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    
    NSMutableArray *nameArray = [NSMutableArray array];
    for (MCSendListNameModel *model in self.dataListArry) {
        [nameArray addObject:model.UserName];
    }

    self.searchList = [NSMutableArray arrayWithArray:[nameArray filteredArrayUsingPredicate:preicate]];
    if ([searchController.searchBar.text isEqualToString:@""]) {
        self.searchList = nameArray;
    }
    [self.tableView reloadData];
}


#pragma mark - getter and setter
- (MCSendListNameModel *)sendListNameModel{
    if (_sendListNameModel == nil) {
        _sendListNameModel = [[MCSendListNameModel alloc] init];
        
    }
    return _sendListNameModel;
}

@end
